//
//  InToChancelViewController.m
//  我的管理
//
//  Created by brother on 16/3/3.
//  Copyright © 2016年 brother. All rights reserved.
//

#import "InToChancelViewController.h"

#import "AppDelegate.h"

#import "InChannelModel.h"

@interface InToChancelViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *myCollectionView;

@property (nonatomic, strong)NSMutableArray *toChannelArray;

@end
static NSString *kscellIdentifier = @"channelCellIDd";

static NSString *chancelcellIdentifier = @"ChancelToCell";

@implementation InToChancelViewController


- (NSMutableArray *)toChannelArray {
    if (_toChannelArray == nil) {
        _toChannelArray = [NSMutableArray array];
    }
    return _toChannelArray;
}
//网络请求
- (void)loadChannelWithUserid:(NSString *)userid withPage:(int)page{
    if ([AppDelegate shareAppdelegate].canConnectedToTheInternet) {

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    NSString *url = [NSString stringWithFormat:@"http://1.wangtest11.applinzi.com/home/Banner/backuserattentionchannel?userid=%@&page=%d",userid,page];
    
    [manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"45645645445445464%@",responseObject);
        
        NSMutableArray *responseObjectArray = responseObject[@"data"];
        
       // NSLog(@"%@",responseObjectArray[0]);
        if (responseObjectArray.count == 0) {
            self.toChannelArray = nil;
        }else {
        
        for (int i = 0; i<responseObjectArray.count; i++) {
            InChannelModel *channelModel = [[InChannelModel alloc]init];
            channelModel.channel_id = responseObjectArray[i][@"channel_id"];
            channelModel.name = responseObjectArray[i][@"name"];
            channelModel.userid = responseObjectArray[i][@"userid"];
            channelModel.pic = responseObjectArray[i][@"pic_100"];
            
            channelModel.pic_500 = responseObjectArray[i][@"pic_500"];
            
            channelModel.share_count = responseObjectArray[i][@"share_count"];
            
            channelModel.follow_count = responseObjectArray[i][@"follow_count"];
            
            channelModel.sound_count = responseObjectArray[i][@"sound_count"];
            

            [self.toChannelArray addObject:channelModel];
        }
        }
        [self.myCollectionView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    }else {
        [self showAlertView:@"当前网络不可用"];
    }
}
#pragma mark - 封装UIAlertView方法，直接调用就可以显示提示
-(void)showAlertView:(NSString *)string {
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:string delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alertView show];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    
    //通过Nib生成cell，然后注册 Nib的view需要继承 UICollectionViewCell
    [self.myCollectionView registerNib:[UINib nibWithNibName:@"InToChancelCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kscellIdentifier];
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    NSString *user_id = [userDefault valueForKey:@"u_id"];
    
    [self loadChannelWithUserid:user_id withPage:1];
    
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)returned:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark -CollectionView datasource
//section
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
//item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.toChannelArray.count == 0) {
        return 1;
    }else {
    return self.toChannelArray.count;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    //重用cell
    InToChancelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kscellIdentifier forIndexPath:indexPath];
    if (self.toChannelArray.count == 0) {
        cell.channelNameLabel.text = @"fhushfgudh";
    }else {
    InChannelModel *channel = self.toChannelArray[indexPath.row];
    cell.channelNameLabel.text = channel.name;
    [cell loadImageWithStr:channel.pic];
    }
    return cell;
    
}

//定义每个UICollectionViewCell 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(self.view.frame.size.width/2.2, self.view.frame.size.height/8);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(10, self.view.frame.size.width/50, 10, self.view.frame.size.width/50);
    
}

//选择了某个cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    InChannelSoundsViewController *controller = [[InChannelSoundsViewController alloc] init];
    
    __weak typeof (self) temp = self;
    
    if (self.toChannelArray.count != 0) {
        //        NSDictionary *channel = [NSDictionary dictionary];
        
        InChannelModel *channelModel = self.toChannelArray[indexPath.row];
        
        NSDictionary *channel = @{@"id":channelModel.channel_id,@"pic_500":channelModel.pic_500,@"share_count":channelModel.share_count,
                                  @"sound_count":channelModel.sound_count,@"follow_count":channelModel.follow_count};
        
        // [channel setValue:channelModel.channel_id forKey:@"id"];
        
        NSLog(@"   pic_500 %@",channelModel.pic_500);
        
        controller.dic = channel;
        //push用在UINavigationController下得语句
        [temp presentViewController:controller animated:YES completion:nil];
        //    [temp.navigationController pushViewController:controller animated:YES];
        
    }
    
 
}

//取消选择了某个cell
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    [cell setBackgroundColor:[UIColor whiteColor]];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
